package band.wukong.practice.java.core.pack.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Desc: 正则表达式的四种用途: 查询、提取、分割、替换/删除
 * Refer: http://www.codeceo.com/article/java-date-operation.html
 *
 * @author wukong(wukonggg@139.com)
 */
public class CalendarSample {

    public static void main(String[] args) {
//        aboutValue();
//        aboutConvert();
//        aboutAddAndMinus();
        aboutCompare();
    }

    /**
     * 1. 日期取值
     */
    public static void aboutValue() {
        //有多个重载方法创建 Calendar 对象
        Calendar now = Calendar.getInstance(); //默认
//        //指定时区和地区，也可以只输入其中一个参数
//        Calendar now = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINA);
        print(now);
        printMore(now);
    }

    /**
     * 2. 日期转换
     */
    public static void aboutConvert() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //日期转字符串
            Calendar calendar = Calendar.getInstance();
            String dateString = sdf.format(calendar.getTime());
            System.out.println("dateString = " + dateString);

            //字符串转日期
            Date dateParsed = sdf.parse(dateString);
            System.out.println("dateParsed = " + dateParsed);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 3. 日期加减
     */
    public static void aboutAddAndMinus() {
        // 根据现在时间计算
        Calendar now = Calendar.getInstance();
        System.out.println("now.getTime() = " + now.getTime());
        now.add(Calendar.YEAR, 1); //现在时间的1年后
        System.out.println("now.getTime() = " + now.getTime());
        now.add(Calendar.YEAR, -1); //现在时间的1年前
        System.out.println("now.getTime() = " + now.getTime());

        // 根据某个特定的时间 date (Date 型) 计算
        Calendar specialDate = Calendar.getInstance();
        specialDate.setTime(new Date()); //注意在此处将 specialDate 的值改为特定日期
        System.out.println("specialDate.getTime() = " + specialDate.getTime());
        specialDate.add(Calendar.YEAR, 1); //特定时间的1年后
        System.out.println("specialDate.getTime() = " + specialDate.getTime());
        specialDate.add(Calendar.YEAR, -1); //特定时间的1年前
        System.out.println("specialDate.getTime() = " + specialDate.getTime());

        // 计算两个时间的间隔，例如计算 2016 年 1 月 1 日距离现在有多少天。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = "2016-01-01 11:11:11";
        Calendar calendar = Calendar.getInstance();
        long nowDate = calendar.getTime().getTime(); //Date.getTime() 获得毫秒型日期
        try {
            long specDate = sdf.parse(dateString).getTime();
            long betweenDate = (specDate - nowDate) / (1000 * 60 * 60 * 24); //计算间隔多少天，则除以毫秒到天的转换公式
            System.out.print(betweenDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 4. 日期比较
     */
    public static void aboutCompare() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString_01 = "2016-01-01 11:11:11";
        String dateString_02 = "2016-01-02 11:11:11";
        try {
            Date date_01 = sdf.parse(dateString_01);
            Date date_02 = sdf.parse(dateString_02);
            System.out.println(date_01.before(date_02)); //true，当 date_01 小于 date_02 时，为 true，否则为 false
            System.out.println(date_02.after(date_01)); //true，当 date_02 大于 date_01 时，为 true，否则为 false
            System.out.println(date_01.compareTo(date_02)); //-1，当 date_01 小于 date_02 时，为 -1
            System.out.println(date_02.compareTo(date_01)); //1，当 date_02 大于 date_01 时，为 1
            System.out.println(date_02.compareTo(date_02)); //0，当两个日期相等时，为 0
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private static void print(Calendar c) {
        Date date = c.getTime(); //直接取得一个 Date 类型的日期
        System.out.println("date = " + date);
    }

    private static void printMore(Calendar c) {
        int year = c.get(Calendar.YEAR);
        System.out.print(", year = " + year);
        int month = c.get(Calendar.MONTH) + 1; //当前月，注意加 1
        System.out.print(", month = " + month);
        int day = c.get(Calendar.DATE); //当前日
        System.out.print(", day = " + day);
        int hour = c.get(Calendar.HOUR);    //12小时制的小时数
        System.out.print(", hour = " + hour);
        int hourOfDay = c.get(Calendar.HOUR_OF_DAY);    //24小时制的小时数
        System.out.print(", hourOfDay = " + hourOfDay);
        int minute = c.get(Calendar.MINUTE);    //分钟
        System.out.print(", minute = " + minute);
        int second = c.get(Calendar.SECOND);
        System.out.print(", second = " + second);

        System.out.println();

        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        System.out.print("dayOfWeek = " + dayOfWeek);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); //日期，和 Calendar.DATE 相同
        System.out.print(", dayOfMonth = " + dayOfMonth);

    }


    public final void set(Calendar c) {
        //只设定某个字段的值
        // public final void set(int field, int value)
        c.set(Calendar.YEAR, 2016);

        //设定年月日或者年月日时分或年月日时分秒
        // public final void set(int year, int month, int date[, int hourOfDay, int minute, int second])
        // c.set(2016, 1, 1[, 11, 1, 1]);
        c.set(2016, 1, 1);
        c.set(2016, 1, 1, 11, 1, 1);


        //直接传入一个 Date 类型的日期
        // public final void setTime(Date date)
        c.setTime(new Date());

    }


}
